package com.woniu.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.Map;

/**
 * @author: 16327
 * @date: 2020/12/28 14:17
 * @description:
 */
public class MyInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
       /* int minute = LocalDateTime.now().getMinute();
        if (minute%2==1){
            System.out.println("时间是奇数，拦截");
            return false;
        }
        Map<String, String[]> parameterMap = request.getParameterMap();
        Collection<String[]> values = parameterMap.values();
        for (String[] value : values) {
            for (int i = 0; i < value.length; i++) {
                if (value[i].contains("张三")){
                    System.out.println("包含张三。拦截");
                    return false;
                }
            }
        }*/
        System.out.println("执行preHandle");
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        if (modelAndView.getViewName() != null||modelAndView.getViewName().equals("")) {
            System.out.println("执行postHandle,去哪个页面："+modelAndView.getViewName());
        }else {
            System.out.println("执行postHandle,去哪个页面：");
        }
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("执行afterCompletion,异常是"+ex);
    }
}
